from unittest import TestCase
from ddt import ddt, data, unpack
from time import sleep

from selenium.webdriver.common.by import By

from TEST.FuncLogic.Page_web_登录 import Login
from TEST.FuncLogic.Page_web_交易记录 import TranRecord_tt as TR
from TEST.Data.Data_时间栏 import tt_Date
from TEST.Head.Page import Page
from TEST.Head.Page import Qscreen

@ddt
class TestLoan(TestCase):
    username = 'Mineways'
    password = 'tang991108'

    # # 时间栏前后索引
    # def tt_func_lr(self, site='div[1]/a[1]', time=20, ret=2):
    #     try:
    #         # driver信息
    #         driver = Page().driver
    #         # 登陆
    #         Login(driver).login(self.username, self.password)
    #         # 定位
    #         action = TR(driver)
    #         for i in range(ret):
    #             action.tr_timetable_func('div[1]/a[2]')
    #         # 执行
    #         for i in range(time):
    #             action.tr_timetable_func(site)
    #         sleep(0.5)
    #         # 关闭页面
    #         driver.quit()
    #         return 0
    #     except:
    #         Qscreen(driver)
    #
    # # 测试时间栏向后索引
    # @data(*tt_Date().button())
    # @unpack
    # def test_tt_func_right(self, time=20, ret=2):
    #     flag = self.tt_func_lr(site='div[1]/a[3]', time=time, ret=ret)
    #     self.assertEqual(0, flag)
    #
    # # 测试时间栏向前索引
    # @data(*tt_Date().button())
    # @unpack
    # def test_tt_func_left(self, time=20, ret=2):
    #     print(time,ret)
    #     flag = self.tt_func_lr(site='div[1]/a[1]', time=time, ret=ret)
    #     self.assertEqual(0, flag)

    # 测试时间栏 选择日期-2000-2-28
    def test_tt_date_select(self, year='2022', mouth='2月', day='27', flag=1):
        try:
            # driver信息
            driver = Page().driver
            # 登陆
            Login(driver).login(self.username, self.password)
            # 定位
            action = TR(driver)
            # 回退至年份
            while flag != 0:
                action.tr_timetable_func('div[1]/a[2]')
                action.tr_timetable_func('div[1]/a[2]')
            # 选择相应年月日
                sleep(1)
                for i in range(2, 11):
                    Year = action.tr_timetable_check('div[2]/div[1]/a[' + str(i) + ']')
                    if Year == year:
                        action.tr_timetable_func('div[2]/div[1]/a[' + str(i) + ']')
                        sleep(0.5)
                        for j in range(1,13):
                            Mouth = action.tr_timetable_check('div[2]/div[1]/a[' + str(j) + ']')
                            if Mouth == mouth:
                                action.tr_timetable_func('div[2]/div[1]/a[' + str(j) + ']')
                                sleep(0.5)
                                for k in range(1, 42):
                                    Day = action.tr_timetable_check('div[2]/div[2]/a[' + str(k) + ']')
                                    if Day == day:
                                        action.tr_timetable_func('div[2]/div[2]/a[' + str(k) + ']')
                                        flag = 0
                                        break
                                break
                        break
                if flag == 1:
                    action.tr_timetable_func('div[1]/a[1]')

            # 数据整理
            text = driver.find_element(By.XPATH,
                                          '/html/body/div[1]/div/div[2]/div/div/div[1]/div/div[3]/ul/li[1]/div[2]/input[1]')
            result = text.get_attribute('data-date')
            Mouth = Mouth.strip('月').zfill(2)
            expect = f'{Year}-{Mouth}-{Day}'
            # 关闭页面
            driver.quit()
        except:
                Qscreen(driver)

        self.assertEqual(expect, result)
